package org.linuxsogood.boot.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.UUID;

import static org.quartz.TriggerBuilder.newTrigger;

public class TestQuartzManager {

    public static String JOB_NAME = "动态任务调度";
    public static String TRIGGER_NAME = "动态任务触发器";
    public static String JOB_GROUP_NAME = "JOB_GROUP";
    public static String TRIGGER_GROUP_NAME = "JOB_GROUP";

    private static boolean checkCron(String cron) {
        if (cron == null || cron.isEmpty()) {
            return false;
        }
        String[] crons = cron.split("---");
        for (String c : crons) {
            if (!CronExpression.isValidExpression(c)) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) throws SchedulerException, InterruptedException {
        System.out.println(checkCron("1,3,15,22 * * * * ? *---45,51,55,59 * * * * * *"));
    }

    private static void oneJobMutilTrigger() throws SchedulerException, InterruptedException {
        JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job", "job group").storeDurably().build();

        Trigger trigger = newTrigger()
                .withIdentity("trigger", "222")
                .forJob(job).
                withSchedule(CronScheduleBuilder.cronSchedule("0,5,15 * * * * ?"))
                .startNow()
                .build();

        Trigger trigger1 =  newTrigger().
                withIdentity("trigger1", "333")
                .forJob(job)
                .withSchedule(CronScheduleBuilder.cronSchedule("30,35,40 * * * * ?"))
                .startNow()
                .build();

        Trigger trigger2 = newTrigger()
                .withIdentity("trigger2", "444")
                .forJob(job).
                withSchedule(CronScheduleBuilder.cronSchedule("55,59 * * * * ?"))
                .startNow()
                .build();
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();

        /*TriggerBuilder<CronTrigger> builder = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("55,59 * * * * ?"))
                .withSchedule(CronScheduleBuilder.cronSchedule("30,35,40 * * * * ?"))
                .withSchedule(CronScheduleBuilder.cronSchedule("0,5,15 * * * * ?"));*/

        scheduler.addJob(job, true);
        scheduler.scheduleJob(trigger);
        scheduler.scheduleJob(trigger1);
        scheduler.scheduleJob(trigger2);

        scheduler.start();
        Thread.sleep(10000);
        System.out.println("开始移除任务");
        scheduler.deleteJob(JobKey.jobKey("job", "job group"));
        System.out.println("移除任务完成.");
    }

    private static void method4() {
        System.out.println("【系统启动】开始(每1秒输出一次)...");
        String cron = "1,5,10,15 * * * * ? *---35,40,45,50,55 * * * * ? *";
        String[] cronArr = cron.split("---");
        for (String c : cronArr) {
            String uuid = UUID.randomUUID().toString();
            org.linuxsogood.demo.cron.QuartzManager.addJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME + uuid, TRIGGER_GROUP_NAME + uuid, MyJob.class, c);
        }

        try {
            System.out.println("【移除定时】开始...");
            Thread.sleep(50000);
            System.out.println("【移除定时】成功");
            org.linuxsogood.demo.cron.QuartzManager.removeWithoutTrigger(JOB_NAME, JOB_GROUP_NAME);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void testBaseCRUD() {
        try {
            System.out.println("【系统启动】开始(每1秒输出一次)...");
            org.linuxsogood.demo.cron.QuartzManager.addJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME, MyJob.class, "0/1 * * * * ?");

            Thread.sleep(5000);
            System.out.println("【修改时间】开始(每5秒输出一次)...");
            org.linuxsogood.demo.cron.QuartzManager.modifyJobTime(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME, "0/5 * * * * ?");

            Thread.sleep(6000);
            System.out.println("【移除定时】开始...");
            org.linuxsogood.demo.cron.QuartzManager.removeJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME);
            System.out.println("【移除定时】成功");

            org.linuxsogood.demo.cron.QuartzManager.shutdownJobs();
            Thread.sleep(3000);
            org.linuxsogood.demo.cron.QuartzManager.addJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME, MyJob.class, "0/1 * * * * ?");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}